﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SICON_INT.PaginasWebs
{
    public partial class frm_EsqPlanta : System.Web.UI.Page
    {
        /// <summary>
        /// The form accion
        /// </summary>
        static string FormAccion = "";
        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The codigo
        /// </summary>
        static int codigo = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["FormAccion"] != null)
                {
                    FormAccion = Session["FormAccion"].ToString();

                }


                if (Session["Codigo"] != null)
                {
                    codigo = Convert.ToInt32(Session["Codigo"]);

                }


                if (!IsPostBack)
                {

                    if (FormAccion == "Editar")
                    {
                        if (codigo != 0)
                        {
                            CargarEditar();
                        }
                    }

                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        private void CargarEditar()
        {
            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            WSGestion.MOD_EsqPlanta objEsqPlanta = new WSGestion.MOD_EsqPlanta();
            objEsqPlanta = wsgest.SEL_EsqPlantaByID(codigo);
            txtNombreEsquema.Value = objEsqPlanta.ESQ_nombreesqplanta;
        }

        protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (FormAccion == "Agregar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_EsqPlanta EsqPlantas = new WSGestion.MOD_EsqPlanta();

                    EsqPlantas.ESQ_nombreesqplanta = txtNombreEsquema.Value;

                    int cant_usu = wsgest.SEL_EsqPlantaByEsqPlanta(txtNombreEsquema.Value);
                    if (cant_usu <= 0)
                    {
                        if (wsgest.CRE_EsqPlanta(EsqPlantas))
                        {
                            //Response.Redirect("GridUsuarios.aspx");
                            Server.Transfer("frm_GridEsqPlanta.aspx");
                        }
                        else
                        {
                            MensajeError = "Procedimiento Almacenado no aceptado";
                        }
                    }
                    else
                    {
                        MensajeError = "El Esquema de Planta ya existe";
                    }

                }

                if (FormAccion == "Editar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_EsqPlanta EsqPlantas = new WSGestion.MOD_EsqPlanta();
                    EsqPlantas.ESQ_codesqplanta = codigo;
                    EsqPlantas.ESQ_nombreesqplanta = txtNombreEsquema.Value;


                    wsgest.UPD_EsqPlanta(EsqPlantas);
                    //Response.Redirect("GridUsuarios.aspx");
                    Server.Transfer("frm_GridEsqPlanta.aspx");
                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Server.Transfer("frm_GridEsqPlanta.aspx");
        }

    }
}